package org.dromara.common.core.exception.base

import org.dromara.common.core.utils.MessageUtils
import org.dromara.common.core.utils.StringUtils
import java.io.Serial

/**
 * 基础异常
 *
 * @author ruoyi
 * @updater LikeYouDo
 * @date 2025/1/13 10:32
 */
open class BaseException(
    /**
     * 所属模块
     */
    var module: String? = null,

    /**
     * 错误码
     */
    var code: String? = null,

    /**
     * 错误码对应的参数
     */
    var args: Array<out Any>? = null,

    /**
     * 错误消息
     */
    var defaultMessage: String? = null
) : RuntimeException() {

    companion object {
        @Serial
        private val serialVersionUID: Long = 1L
    }

    constructor(module: String?, code: String?, args: Array<Any>?)
            : this(module, code, args, null)

    constructor(module: String?, defaultMessage: String?)
            : this(module, null, null, defaultMessage)

    constructor(code: String?, args: Array<Any>?)
            : this(null, code, args, null)

    constructor(defaultMessage: String?)
            : this(null, null, null, defaultMessage)

    override val message: String?
        get() {
            var message: String? = null
            if (StringUtils.isNotEmpty(code)) {
                message = MessageUtils.message(code!!, args)
            }
            if (message.isNullOrBlank()) {
                message = defaultMessage
            }
            return message
        }

}
